﻿#include "friendlistwidget.h"
#include "ui_friendlistwidget.h"
#include <QMessageBox>
#include <QGraphicsDropShadowEffect>
#include "utils.h"
#include <QInputDialog>
#include "addfrienddialog.h"

FriendListWidget::FriendListWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FriendListWidget)
{
    ui->setupUi(this);

    // // 设置右键菜单策略
    ui->listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    // // 连接右键菜单信号
    connect(ui->listWidget, &QListWidget::customContextMenuRequested, this, &FriendListWidget::slot_showContextMenu);

    // 设置listwidget 样式
    ui->listWidget->setStyleSheet(
        "QListWidget#listWidget{"
        "background-color:rgb(255,255,255);"
        "border:0px solid black;"
        "}"
        );

    // 设置上方菜单样式widget样式
    ui->widget->setStyleSheet(

        "QWidget#widget{"
        "background-color:rgb(255,255,255);"
        "border:0px solid white;"
        "}"
        "QPushButton{"
        "border:0px solid white;"
        "padding:5px;"
        "border-radius:8px;"
        "}"
        "QPushButton:hover {"
        "border: 0px solid rgb(0,107,190);"
        " background-color: rgb(224,238,249);"
        "color: black;"
        " }"
        "QPushButton:pressed {"
        "border: 0px solid rgb(0,84,154);"
        "background-color: rgb(204,228,247);"
        "color: black;"
        "}"

        );

    // test

    QStringList avatarList;
    QStringList usrNameList;
    QStringList MsgList;
    QStringList msgNumList;
    QStringList timeList;
    avatarList = QStringList()<<"../../avatar/1.jpg"<<"../../avatar/6.jpg"
        <<"../../avatar/2.jpg"<<"../../avatar/3.jpg"
        <<"../../avatar/4.jpg"
                               <<":/img/avatar.png";
    MsgList = QStringList()<<"过去的已经过去了，死了的已经死了，活着的还要继续活着。"
                            <<"所有随风而逝的都属于昨天的，所有历经风雨留下来的才是面向未来的。"
                            <<"有一种东西不能遵循从众原则，那就是人的良心。"
                            <<"世界上只有少数人能够最终达到自己的理想。"
                            <<"为你，千千万万遍。"
                            <<"以欢喜之心，慢度日常。";
    usrNameList = QStringList()<<"池鱼"<<"栀子浅川"<<"碎片储存罐"<<"九月诗酒"<<"花生日记"<<"Allennorth";
    msgNumList = QStringList()<<"6"<<"8"<<"9"<<"67"<<"12"<<"34";
    timeList = QStringList()<<"08-08 20:12"<<"08-09 20:12"<<"08-12 20:12"
                             <<"08-11 20:12"<<"08-12 20:12"<<"08-12 20:12";
    addFriendList(avatarList,MsgList,usrNameList,msgNumList,timeList);

}

FriendListWidget::~FriendListWidget()
{
    delete ui;
}

void FriendListWidget::addFriendList(QStringList avatarList, QStringList MsgList, QStringList usrNameList, QStringList msgNumList,QStringList timeList)
{
    QListWidgetItem* m_pLWItem =NULL;
    FriendListItem* m_pFriendItem = NULL;
    if(avatarList.size() != MsgList.size()
        || MsgList.size() != usrNameList.size()
        || usrNameList.size() != msgNumList.size()
        || msgNumList.size() != timeList.size()){
        return;
    }
    for(int i =0; i < avatarList.size(); i ++){
        m_pLWItem = new QListWidgetItem(ui->listWidget);
        m_pFriendItem = new FriendListItem();
        mList_pFriendItem.append(m_pFriendItem);
        m_pLWItem->setSizeHint(QSize(300,80));
        ui->listWidget->setItemWidget(m_pLWItem,m_pFriendItem);

        m_pFriendItem->setAvatar(avatarList[i], 10);
        m_pFriendItem->setMsg(MsgList[i]);
        m_pFriendItem->setUsrName(usrNameList[i]);
        m_pFriendItem->setNewMsgNum(msgNumList[i]);
        m_pFriendItem->setTime(timeList[i]);

    }
}

void FriendListWidget::slot_showContextMenu(const QPoint &pos)
{
    contextMenu = new QMenu();

    actionDelete = new QAction(tr("删除好友"), contextMenu);
    connect(actionDelete, &QAction::triggered, this, &FriendListWidget::deleteFriend);

    actionMute = new QAction(tr("免打扰"),contextMenu);
    connect(actionMute, &QAction::triggered, this, &FriendListWidget::muteFriend);

    contextMenu->addAction(actionDelete);
    contextMenu->addSeparator();
    contextMenu->addAction(actionMute);


    // 设置菜单样式
    contextMenu->setStyleSheet(
        "QMenu {"
        "    color: black;"
        "    background-color: white;"
        "    border-radius: 10px;"
        "    padding: 8px;"
        "    margin: 2px;"
        "}"
        "QMenu::item {"
        "    border-radius: 20px;"
        "}"
        "QMenu::item:text {"
        "    padding-top: 8px;"
        "    padding-bottom: 8px;"
        "    padding-left: 25px;"
        "    padding-right: 25px;"
        "}"
        "QMenu::item:selected {"
        "    color: #1aa3ff;"
        "    background-color: #e5f5ff;"
        "    border-radius: 8px;"
        "}"
        "QMenu::separator {"
        "    height: 1px;"
        "    background: #bbbbbb;"
        "    margin: 5px;"
        "    margin-left: 10px;"
        "    margin-right: 10px;"
        "}"
        );

    // contextMenu->setWindowFlags(contextMenu->windowFlags()  | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
    // contextMenu->setAttribute(Qt::WA_TranslucentBackground,true);
    // QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(contextMenu);
    // shadow->setOffset(0, 0);
    // shadow->setColor(QColor("#444444"));
    // shadow->setBlurRadius(10);
    // contextMenu->setGraphicsEffect(shadow);

    contextMenu->exec(ui->listWidget->mapToGlobal(pos));
    delete contextMenu;

}

void FriendListWidget::deleteFriend() {
    // 获取当前选中的项并删除
    QListWidgetItem *item = ui->listWidget->currentItem();
    if (item) {
        delete item;
    }
}

void FriendListWidget::muteFriend() {
    // 获取当前选中的项并显示免打扰提示
    QListWidgetItem *item = ui->listWidget->currentItem();
    if (item) {
        QMessageBox::information(this, tr("免打扰"), tr("已对 %1 开启免打扰").arg(item->text()));
    }
}

void FriendListWidget::on_pbn_AddFriend_clicked()
{
    AddFriendDialog::getInstance()->show();
}

